package com.javafactroy.kunlong.client.feign.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.javafactroy.kunlong.client.feign.consts.ResponseCode;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Transient;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;

@Data
@NoArgsConstructor
public class ApiResult<T> implements Serializable {
    //@Serial
    private static final long serialVersionUID = 1L;
    private Integer code;

    private String msg;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String sysMsg;

    private T data;
    private List<T> dataList;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Integer total;
    @Transient
    @JsonInclude(JsonInclude.Include.NON_NULL)
    protected Integer pageSize;
    @Transient
    @JsonInclude(JsonInclude.Include.NON_NULL)
    protected Integer pageNum;

    public ApiResult(Integer code, String message) {
        this.code = code;
        this.msg = message;
        this.dataList = new ArrayList<>();
    }

    public ApiResult(Integer code, String message, T data) {
        this(code, message);
        this.data = data;
    }

    public static <T> ApiResult<T> success() {
        return new ApiResult<>(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMessage());
    }

    public static <T> ApiResult<T> success(T data) {
        return new ApiResult<>(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMessage(), data);
    }

    public static <T> ApiResult<T> fail() {
        return new ApiResult<>(ResponseCode.FAILURE.getCode(), ResponseCode.FAILURE.getMessage());
    }

    public static <T> ApiResult<T> fail(String msg) {
        return new ApiResult<>(ResponseCode.FAILURE.getCode(), msg);
    }

    public static <T> ApiResult<T> fail(Integer code, String message) {
        return new ApiResult<>(code, message);
    }

    public static <T> ApiResult<T> error() {
        return new ApiResult<>(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getMessage());
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Integer getCode() {
        return this.code;
    }

    public String getMsg() {
        return this.msg;
    }

    public T getData() {
        return this.data;
    }

    @Override
    public String toString() {
        return (new StringJoiner(", ", ApiResult.class.getSimpleName() + "[", "]"))
                .add("code=" + this.code)
                .add("message='" + this.msg + "'")
                .add("data=" + this.data)
                .toString();
    }
}

